var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		userinfo: [],
		money: '',
		moneyduan:0
	},
	_onLoad: function (options) {
		var that = this
		wx.setNavigationBarTitle({
			title: (that.data.textset['余额'] || '余额')+'充值'
		});
		$.get('wxpay/recharge',{},function(res){
			if(res.canrecharge == 0){
				$.goto('/pages/my/moneylog?st=0','redirect');return;
			}
			that.setData(res)
		})
	},
	moneyinput: function (e) {
		var money = e.detail.value
		var giveset = this.data.giveset
		if (parseFloat(money) < 0) {
			$.error('必须大于0');
		} else {
			var moneyduan = 0;
			if(giveset.length > 0){
				for(var i in giveset){
					if(money*1 >= giveset[i]['money']*1 && giveset[i]['money']*1 > moneyduan){
						moneyduan = giveset[i]['money']*1
					}
				}
			}
			console.log(moneyduan)
			this.setData({ money: money,moneyduan:moneyduan})
		}
	},
	selectgiveset:function(e){
		var money = e.currentTarget.dataset.money;
		this.setData({ money: money,moneyduan:money})
	},
	wxpay: function (e) {
		var that = this
		var money = e.detail.value.money;
		$.post('wxpay/recharge', { money: money }, function (data) {
			if (data.status == 0) {
				$.error(data.msg);
				return;
			}
			that.setData({give_coupon_list:data.give_coupon_list})
			var opt = data.data;
			if($.platform == 'wx'){
				wx.requestPayment({
					'timeStamp': opt.timeStamp,
					'nonceStr': opt.nonceStr,
					'package': opt.package,
					'signType': 'MD5',
					'paySign': opt.paySign,
					'success': function (res) {
						$.success('付款完成');
						setTimeout(function () {
							if(that.data.give_coupon_list && that.data.give_coupon_list.length > 0){
								that.setData({give_coupon_show:true,give_coupon_close_url:'/pages/my/usercenter'})
							}else{
								$.goto('/pages/my/usercenter');
							}
						}, 1000)
					},
					'fail': function (res) {

					}
				})
			}else{
				tt.pay({
					orderInfo: opt,
					service: 3,
					getOrderStatus:function(res){
							var out_order_no = res;
						return new Promise(function(resolve) {
								resolve({ code: 9 });//0：支付成功 1：支付超时 2：支付失败 3：支付关闭 9：订单状态未知/未支付
						});
					},
					success:function(res) {
						if (res.code == 0) { //支付成功
							$.success('付款完成');
							setTimeout(function () {
								if(that.data.give_coupon_list && that.data.give_coupon_list.length > 0){
									that.setData({give_coupon_show:true,give_coupon_close_url:'/pages/my/usercenter'})
								}else{
									$.goto('/pages/my/usercenter');
								}
							}, 1000)
						}
						if (res.code == 9) { //支付状态未知
							//$.success('付款完成');
							setTimeout(function () {
								$.goto('/pages/my/usercenter');
							}, 1000)
						}
					},
					fail:function(res) {console.log('fail');}
				});
			}
		})
	}
}))